var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    levle:'',
    one_id:'',
    "bnrUrl": [{
      "url": "../../images/lunbo1.jpg"
    }, {
      "url": "../../images/lunbo3.jpg"
    }],
   
    'array': [{ src: "../../images/guide-1.png", text: "新手指南" },
    { src: "../../images/guide-2.png", text: "我的收藏" },
    { src: "../../images/guide-3.png", text: "常见问题" },
    { src: "../../images/guide-4.png", text: "系统消息" }],

    tuangou_icon: '../../images/group.png',
    tuangou_good_img: 'http://zkpic.huiyunbao.cn/pic/cardImage/三轮秒杀图.jpg吕.jpg',
    

    miaosha_icon: '../../images/spike.png',
    miaosha_good_img: 'http://zkpic.huiyunbao.cn/pic/cardImage/三轮秒杀图.jpg吕.jpg',
  
  },

  onLoad: function (options){
    console.log("options:", options)
    //扫码进来之后获得后台传过来的参数
    if(options.scene){
      var scene = decodeURIComponent(options.scene)
      var arrPara = scene.split("#");

      var oneid = arrPara[0]
      var levle = arrPara[1]
      var flag = arrPara[2]


      this.setData({
        one_id: oneid,
        levle: levle,
        flag : flag
      })
    }
    
    wx.hideHomeButton() 
  },
  bindGetUserInfo: function (e) {
    var that = this
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      console.log("用户的信息如下：");
      console.log(e.detail.userInfo);

      var user = e.detail.userInfo;

      var headimgurl = user.avatarUrl

      user.headimgurl = headimgurl
      
      wx.login({
        success: res => {
          // 获取到用户的 code 之后：res.code
          console.log("用户的code:" + res.code);
          // 可以传给后台，再经过解析获取用户的 openid
          var data = {
            user: user,
            code: res.code
          }
          console.log('data:', data)

          wx.request({
            url: app.globalData.URL + '/app/WeChatOAuth/saveUser',
            method: 'post',
            data: JSON.stringify(data),
            header: {
              "Content-Type": "application/json;charset=utf-8"
            },
            success: function (res) {
              console.log("saveUser", res.data)
              wx.setStorage({
                key: "openid",
                data: res.data.openid
              })

              var one_id = that.data.one_id //提供二维码人的openid
              var second_id = res.data.openid //本人的openid
              var levle = that.data.levle

              if (that.data.flag == 1){
                wx.request({
                  url: app.globalData.URL + '/app/WeChatOAuth/savePersonal?one_id=' + one_id + '&second_id=' + second_id + '&level=' + level,
                  success: function (res) {
                    console.log("savePersonal", res.data)
                  }
                })
              }
              

              if (res.data.openid){
                wx.switchTab({
                  url: '../index/index',
                })
              }

            }
          })

        }
      });
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      that.setData({
        isHide: false
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
  
})